package com.shop.shoppingmall.controller;

import com.shop.shoppingmall.dao.ShUserDao;
import com.shop.shoppingmall.entity.ShUser;
import com.shop.shoppingmall.entity.ShVip;
import com.shop.shoppingmall.service.ShVipService;
import com.shop.shoppingmall.util.CommonConstant;
import com.shop.shoppingmall.util.JwtUtil;
import com.shop.shoppingmall.util.api.vo.Result;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * vip状态(ShVip)表控制层
 *
 * @author makejava
 * @since 2020-09-05 14:22:41
 */
@RestController
@RequestMapping("shVip")
@CrossOrigin
public class ShVipController {
    /**
     * 服务对象
     */
    @Resource
    private ShVipService shVipService;
    @Resource
    private ShUserDao shUserDao;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public ShVip selectOne(String id) {
        return this.shVipService.queryById(id);
    }

    @GetMapping("queryUserId")
    public Result queryUserId(HttpServletRequest request) {
        String name1 = JwtUtil.getUserNameByToken(request);
        ShUser shUser = shUserDao.selectLoginName(name1);
        if(shUser == null || name1 == null){
            return Result.error(CommonConstant.SC_OK_201,"您还未注册或者登录，请先注册或登录");
        }
        return shVipService.queryByUserId(shUser.getId());
    }


    public void updateByUserId(String userId) {
         shVipService.updateByUserId(userId);
    }

}
